<?php
namespace App\Library;
use Illuminate\Support\Facades\Redis;


trait ChargeCache
{
    protected $prefix = 'charge:evcs_charge_';          //充电设备状态

    protected $prefixOrderNo = 'charge:evcs_order_';    //充电订单信息

    /**
     * 获取充电状态
     * @param $connectId
     * @return mixed
     */
    public function getChargeStatusCache($connectId)
    {
        return Redis::get($this->prefix . $connectId);
    }

    /**
     * 设置充电状态
     * @param $connectId
     * @param $status
     * @return bool
     */
    public function setChargeStatusCache($connectId, $status, $startChargeSeq)
    {
        return Redis::set($this->prefix . $connectId, json_encode(['status' => $status, 'StartChargeSeq' => $startChargeSeq]));
    }

    /**
     * 设置充电状态
     * @param $connectId
     * @param $startSeqStart
     * @return void
     */
    public function delCache($connectId, $startSeqStart)
    {
        Redis::del($this->prefix . $connectId);
        Redis::del($this->prefixOrderNo . $startSeqStart);
    }

    /**
     * 设置充电开始时间
     * @param $startSeqStart
     * @param $data
     * @return mixed
     */
    public function setChargeCacle($startSeqStart, $data)
    {
        return Redis::set($this->prefixOrderNo . $startSeqStart, json_encode($data));
    }

    /**
     * 获取充电任务
     * @param $startSeqStart
     * @return mixed
     */
    public function getChargeCacle($startSeqStart)
    {
        return Redis::get($this->prefixOrderNo . $startSeqStart);
    }

    /**
     * 清理缓存
     * @return void
     */
    public function delAll()
    {
        return Redis::command('flushall');
    }

    /**
     * 设置key
     * @param $key
     * @param $val
     * @return mixed
     */
    public function setKey($key, $val)
    {
        return Redis::set($key, $val);
    }

    /**
     * 获取key
     * @param $key
     * @return mixed
     */
    public function getKey($key)
    {
        return Redis::get($key);

    }
}
